﻿<%@ WebHandler Language="C#" Class="AspxFileUpload" %>

using System;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

public class AspxFileUpload : IHttpHandler
{
    private const int UploadFileLimit = 3;//上传文件数量限制

    private string _msg = "上传成功！";//返回信息

    public void ProcessRequest(HttpContext context)
    {
        int iTotal = context.Request.Files.Count;
        String FilePathStr = null;
        if (iTotal == 0)
        {
            _msg = "没有数据";
        }
        else
        {
            int iCount = 0;

            for (int i = 0; i < iTotal; i++)
            {
                HttpPostedFile file = context.Request.Files[i];
                string strNewFileName = "";
                if (file.ContentLength > 0 || !string.IsNullOrEmpty(file.FileName))
                {
                    //保存文件
                    try
                    {
                        strNewFileName = Path.GetFileName(file.FileName);

                        string unipp = @"[\u4e00-\u9fa5]+";
                        if (Regex.IsMatch(strNewFileName, unipp))
                        {
                            strNewFileName = Caceolod.StringToPinYing(strNewFileName);
                        }
                        
                        string EName = System.IO.Path.GetExtension(file.FileName);
                        string strPath = "";
                        switch (EName.ToLower())
                        {
                            case ".gif":
                            case ".jpg":
                                strPath = "\\AspxPath\\UploadFiles\\Images\\" + strNewFileName;
                                break;
                            default:
                                strPath = "\\AspxPath\\UploadFiles\\Files\\" + strNewFileName;
                                break;

                        }

                        string strFilePath = strPath.Replace("\\", "/");
                        strPath = System.Web.HttpContext.Current.Server.MapPath(strPath);
                        if (!Directory.Exists(Path.GetDirectoryName(strPath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(strPath));
                        }
                        if (File.Exists(strPath))
                        {
                            //文件存在,就重命名文件
                            //1.取得文件扩展名
                            string ExtensionName = System.IO.Path.GetExtension(file.FileName);
                            //2.重名标识
                            string strFlag = DateTime.Now.ToString("FFF");
                            strPath = strPath.Remove(strPath.LastIndexOf('.')) + "(" + strFlag + ")" + ExtensionName;
                            strFilePath = strFilePath.Remove(strFilePath.LastIndexOf('.')) + "(" + strFlag + ")" + ExtensionName;
                        }
                        file.SaveAs(strPath);

                        if (FilePathStr != null)
                        {
                            FilePathStr += "|" + strFilePath;
                        }
                        else
                        {
                            FilePathStr = strFilePath;
                        }
                        
                    }
                    catch (Exception ex)
                    {
                        //context.Response.Write("<script>window.parent.Finish('" + ex.Message + "');</script>"); 
                    }

                    //这里可以根据实际设置其他限制
                    if (++iCount > UploadFileLimit)
                    {
                        _msg = "超过上传限制：" + UploadFileLimit;
                        break;
                    }
                }
            }
        }
        context.Response.Write("<script>top.CheckedFile('"+FilePathStr+"');window.parent.Finish('" + _msg + "');</script>");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}